import $U from "@/common/free-lib/util.js"
import $R from "@/common/free-lib/request.js"
import $C from "@/common/free-lib/config.js"
import chat from "@/common/free-lib/chat.js"
export default {
	state: {
		user: false,
		token: false,
		apply: {
			rows: [],
			count: 0
		},
		mailList: [],
		chat: null,
		chatList: [],
		totalNoreadnum: 0,
		notice:{
			avatar:"",
			user_id:0,
			num:0
		}
	},
	mutations: {
		updateUser(state, {
			k,
			v
		}) {
			if (state.user) {
				state.user[k] = v
				//更新本地用户信息
				$U.setStorage('user', JSON.stringify(state.user))
				uni.$emit('updateUser',state.user)
			}
		}
	},
	actions: {
		//初始化登录状态
		init({
			state,
			dispatch
		}) {
			state.user = $U.getStorage('user') ? JSON.parse($U.getStorage('user')) : false
			state.token = $U.getStorage('token')
			//获取好友申请

			state.chat = new chat({
				url: $C.socketUrl
			})

			//获取会话列表

			dispatch('getApply')
			dispatch('getBadge')
			dispatch('getChatList')
			dispatch('getNotice')
		},
		//登录后处理
		login({
			state,
			dispatch
		}, user) {
			//存到状态中
			state.user = user
			//存储到本地存储中
			$U.setStorage('token', user.token)
			$U.setStorage('user', JSON.stringify(user))
			$U.setStorage('user_id', JSON.stringify(user.id))
			//获取好友申请
			//获取会话列表
			state.chat = new chat({
				url: $C.socketUrl
			})
			
			dispatch('getApply')
			dispatch('getBadge')
			dispatch('getChatList')
			dispatch('getNotice')
		},
		//退出登录
		logout({
			state
		}) {
			state.user = false
			state.token = false
			$U.removeStorage('user')
			$U.removeStorage('token')
			$U.removeStorage('user_id')
			//关闭socket

			if(state.chat){
				state.chat.close()
			}
			state.chat = null
			
			uni.$off('updateBadge')
			uni.$off('momentNotice')
			uni.$off('onUpdateChatList')

		},
		//断线自动重连
		reconnect({state}){
			if(state.user && state.chat){
				state.chat.reconnect()
			}
		},
		//获取好友申请
		getApply({
			state,
			dispatch
		}, page = 1) {
			$R.get('/apply/' + page).then(res => {
				state.apply = res
				//更新通讯录角标
				dispatch('updateMailBadge')
			})
		},
		//获取角标
		getBadge({
			state
		}) {

			state.totalNoreadnum = state.chat.updateBadge()
			//角标全局通知
			uni.$on('updateBadge', (total) => {
				state.totalNoreadnum = total
			})
		},
		//删除好友
		deleteUser({
			state
		}, id) {

			return new Promise((result, reject) => {
				$R.post('/user/deleteUser', {
					friend_id: id
				}).then(res => {

					//清空本地聊天记录
					let key = `chatDetail_${state.user.id}_user_` + id
					// console.log(id, key)
					$U.removeStorage(key)
					//获取所有会话列表
					let list = state.chat.getChatList()
					//找到当前会话
					let index = list.findIndex(item => item.id === id && item.chat_type === 'user')
					if (index !== -1) {
						list.splice(index, 1)
						state.chat.setChatList(list)

						//实时更新
						uni.$emit('onUpdateChatList', list)
					}
					result(res)

				}).catch(err => {
					reject(err)
				})

			})
		},
		updateApply({
			state
		}, data) {
			state.apply = data
		},
		//更新角标
		updateMailBadge({
			state
		}) {
			if (state.apply.count > 0) {
				let count = state.apply.count > 99 ? '99+' : state.apply.count.toString()
				return uni.setTabBarBadge({
					index: 1,
					text: count
				})
			}
			uni.removeTabBarBadge({
				index: 1
			})
		},
		//获取会话列表
		getChatList({
			state
		}) {
			state.chatList = state.chat.getChatList()
			 // console.log(state.chatList)
			//监听会话列表会话
			uni.$on('onUpdateChatList', (list) => {
				state.chatList = list
			})
		},
		//获取朋友圈动态通知
		getNotice({state}){
			state.notice = state.chat.getNotice()
			uni.$on('momentNotice',(res)=>{
				state.notice = res
			})
			
			if(state.notice.num>0){
				uni.setTabBarBadge({
					index:2,
					text:state.notice.num>99?'99+':state.notice.num.toString()
				})
			}else{
				uni.removeTabBarBadge({
					index:2
				})
			}
		}
		
		
	}
}
